package com.converters;


import cn.hutool.core.collection.CollectionUtil;
import com.domain.WriteData;
import com.enums.Alignment;
import com.enums.TextLevEnum;
import com.exception.CollectionParseException;
import com.fonts.AbstractFont;
import com.pdf.annotates.CellSize;
import com.pdf.annotates.PdfProperty;
import com.reflect.ReflectUtil;
import org.apache.commons.lang3.ArrayUtils;

import java.lang.reflect.Field;
import java.util.*;

/**
 * https://www.jb51.net/program/292587jer.htm
 */
public class BeanConverter {

    public static List<WriteData> beanToWriteData(Object bean) {
        List<WriteData> result = new ArrayList();
        Iterator var3;
        if (bean instanceof Map) {
        } else {
            List<Field> fieldList = com.reflect.ReflectUtil.getFieldList(bean.getClass());
            var3 = fieldList.iterator();

            while (var3.hasNext()) {
                Field field = (Field) var3.next();
                PdfProperty pdfProperty = field.getAnnotation(PdfProperty.class);
                if (Objects.isNull(pdfProperty)) {
                    continue;
                }
                String key = !pdfProperty.value().isEmpty() ? pdfProperty.value() : field.getName();
                Alignment alignment = pdfProperty.alignment();
                TextLevEnum levEnum = pdfProperty.lev();
                Converter converter = DefaultConverters.getConverter(pdfProperty.converter().getName());
                Object fieldValue = ReflectUtil.getFieldValue(bean, field);
                checkList(fieldValue, field.getName(), ArrayUtils.isEmpty(pdfProperty.head()));
                WriteData writeData = converter.convertToWriteData(key, fieldValue, alignment, levEnum);
                writeData.setGroup(pdfProperty.group());
                writeData.setRow(pdfProperty.row());
                writeData.setFont(AbstractFont.chooseFont(pdfProperty.font().getName()));
                writeData.setHead(pdfProperty.head());
                String name = pdfProperty.writeHandle().getName();
                List<String> classNameList = Arrays.asList("com.write.DefaultWriteData", "com.write.TableWriteData");
                writeData.setWriteHandleClassName(classNameList.contains(name) ? null : name);
                float[] cellSizeArr = cellSizeArr(field);
                writeData.setCellSizeArray(cellSizeArr);
                if (ArrayUtils.isNotEmpty(cellSizeArr)) {
                    writeData.setChangeCellSizeBoolean(true);
                }
                writeData.setLineEnum(pdfProperty.line());
                writeData.setFont(AbstractFont.chooseFont(pdfProperty.font().getName()));
                result.add(writeData);
            }
        }

        return result;
    }

    /**
     *判断字段为数组，没有指定表头则抛出异常
     * @param fieldValue
     * @param fieldName
     * @param headHasValue
     */
    private static void checkList(Object fieldValue, String fieldName, Boolean headHasValue) {
        if (fieldValue instanceof Iterable) {
            if (CollectionUtil.isEmpty((Collection<?>) fieldValue)) {
                if (headHasValue) {
                    throw new CollectionParseException(String.format("parse column: %s fail， Cause No table header is specified", fieldName));
                }

            }
        }
    }

    private static float[] cellSizeArr(Field field) {
        CellSize cellSize = field.getAnnotation(CellSize.class);
        if (Objects.nonNull(cellSize)) {
            String[] size = cellSize.size();
            float[] cellSizeList = new float[size.length];
            if (ArrayUtils.isNotEmpty(size)) {
                for (int i = 0; i < size.length; i++) {
                    cellSizeList[i] = Float.valueOf(size[i]);
                }
                return cellSizeList;
            }
        }
        return null;
    }


}
